<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!--      <el-form-item label="商品名称" prop="name">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.name"-->
      <!--          placeholder="请输入商品名称"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="商品编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入商品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属客户" prop="customerId">
        <el-input
          v-model="queryParams.customerId"
          placeholder="请输入所属客户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wms:goods:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wms:goods:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wms:goods:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wms:goods:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item v-for="custom in props.row.customContext" :label="custom.label">
              <span>{{ custom.value }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="商品名称" align="center" prop="name"/>
      <el-table-column label="商品编码" align="center" prop="code"/>
      <el-table-column label="所属客户" align="center" prop="customerId"/>
      <el-table-column label="长" align="center" prop="length"/>
      <el-table-column label="宽" align="center" prop="width"/>
      <el-table-column label="高" align="center" prop="height"/>
      <el-table-column label="重量" align="center" prop="weight"/>
      <el-table-column label="体积" align="center" prop="volume"/>
      <el-table-column label="价格" align="center" prop="price"/>
      <el-table-column label="计数单位" align="center" prop="unit"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wms:goods:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wms:goods:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入商品名称"/>
        </el-form-item>
        <el-form-item label="商品编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入商品编码"/>
        </el-form-item>
        <el-form-item label="所属客户" prop="customerId">
          <el-input v-model="form.customerId" placeholder="请输入所属客户"/>
        </el-form-item>
        <el-collapse v-model="activeCollapse">
          <el-collapse-item title="其他录入信息">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注"/>
            </el-form-item>
            <el-form-item label="长" prop="length">
              <el-input v-model="form.length" placeholder="请输入长"/>
            </el-form-item>
            <el-form-item label="宽" prop="width">
              <el-input v-model="form.width" placeholder="请输入宽"/>
            </el-form-item>
            <el-form-item label="高" prop="height">
              <el-input v-model="form.height" placeholder="请输入高"/>
            </el-form-item>
            <el-form-item label="重量" prop="weight">
              <el-input v-model="form.weight" type="Number" placeholder="请输入重量"/>
            </el-form-item>
            <el-form-item label="体积" prop="volume">
              <el-input v-model="form.volume" placeholder="请输入体积"/>
            </el-form-item>
            <el-form-item label="价格" prop="price">
              <el-input v-model="form.price" placeholder="请输入价格"/>
            </el-form-item>
            <el-form-item label="计数单位" prop="unit">
              <el-input v-model="form.unit" placeholder="请输入计数单位"/>
            </el-form-item>
            <el-form-item label="自定义模板ID" prop="customId">
              <custom-select :disabled="!!form.id" v-model="form.customId" @change="customChange"/>
            </el-form-item>
            <el-form-item v-for="custom in customList" :label="custom.label" :prop="custom.prop">
              <!--文本框-->
              <el-input v-model="custom.value" :placeholder="'请输入'+custom.label"/>
              <!--数值框-->
              <!--下拉框-->
              <!--日期框-->
            </el-form-item>
          </el-collapse-item>
        </el-collapse>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listGoods, getGoods, delGoods, addGoods, updateGoods} from "@/api/wms/goods";
import CustomSelect from "@/components/CustomSelect"

export default {
  name: "Goods",
  dicts: ['wms_form_type'],
  components: {CustomSelect},
  data() {
    return {
      activeCollapse: 0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品表格数据
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        code: null,
        customerId: null,
        length: null,
        width: null,
        height: null,
        weight: null,
        volume: null,
        price: null,
        unit: null,
        customId: null,
        customContext: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          {required: true, message: "名称不能为空", trigger: "blur"}
        ],
        code: [
          {required: true, message: "编码不能为空", trigger: "blur"}
        ],
        customerId: [
          {required: true, message: "客户不能为空", trigger: "blur"}
        ],
      },
      customList: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 商品状态修改 */
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.name + '"商品吗？').then(function () {
        let par = {id: row.id, status: row.status}
        return updateGoods(par);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function () {
        row.status = row.status === "0" ? "1" : "0";
      });
    },
    /** 选择商品模板 */
    customChange(val, row) {
      this.customList = JSON.parse(row.context);
    },
    /** 查询商品列表 */
    getList() {
      this.loading = true;
      listGoods(this.queryParams).then(response => {
        this.goodsList = response.rows.map(item => {
          item.customContext = JSON.parse(item.customContext);
          return item;
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        code: null,
        customerId: null,
        length: null,
        width: null,
        height: null,
        weight: null,
        volume: null,
        price: null,
        unit: null,
        customId: null,
        customContext: null,
        status: "0",
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.customList = [];
      this.activeCollapse = 0;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGoods(id).then(response => {
        this.form = response.data;
        if (this.form.customId) {
          this.customList = JSON.parse(this.form.customContext);
        }
        this.open = true;
        this.title = "修改商品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.customContext = JSON.stringify(this.customList);
          if (this.form.id != null) {
            updateGoods(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGoods(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品编号为"' + ids + '"的数据项？').then(function () {
        return delGoods(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wms/goods/export', {
        ...this.queryParams
      }, `goods_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 100%;
}
</style>
